package com.example.bootdemo.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.example.bootdemo.common.result.ResultBody;
import com.example.bootdemo.common.util.ExcelStyleUtil;
import com.example.bootdemo.common.util.ExcelUtiles;
import com.example.bootdemo.common.util.PropertiesUtil;
import com.example.bootdemo.service.EmployeeService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/excel/")
public class ExcelController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 条件查询员工列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "exportRecordInfos", method = RequestMethod.GET)
    public ResultBody listEmployeeByChoose(Integer employeeType, Integer employeeStatus,
                                           String name, Integer departmentId, Integer departmentPid, String inTime,
                                           String endProbationTime) {
        Map infoMap = new HashMap();
        
        infoMap.put("employeeType", employeeType);
        infoMap.put("name", name);
        infoMap.put("employeeStatus", employeeStatus);
        infoMap.put("departmentId", departmentId);
        infoMap.put("departmentPid", departmentPid);
        infoMap.put("inTime", inTime);
        infoMap.put("endProbationTime", endProbationTime);

        ResultBody resultBody = new ResultBody();
        String filename = "员工档案信息表";
        String path = "";
        try {
            //结果数据
            List<Map<String, Object>> mapList = new ArrayList<>();
            Map map = new HashMap();

            map = employeeService.findEmployeeEntryForRecord(infoMap);

            ExportParams exportParams = new ExportParams("员工档案信息表（HR用）", "基本信息");
            exportParams.setStyle(ExcelStyleUtil.class);
            map.put("title", exportParams);
            mapList.add(map);
            Workbook workbook = ExcelUtiles.exportExcelForMapList(mapList, ExcelType.HSSF);
            PropertiesUtil.readProperties("application.properties");
            String originalPath = PropertiesUtil.getProperty("imagePath");
            String excelUrl = PropertiesUtil.getProperty("imageUrl");
            String filePath = "excel";
            File filePathTemp = new File(originalPath + File.separator + filePath + File.separator);
            // 判断文件夹是否存在
            if (!filePathTemp.exists()) {
                // 创建文件夹
                filePathTemp.mkdirs();
            }
            path = originalPath + File.separator + filePath + File.separator + filename + System.currentTimeMillis() + ".xls";
            FileOutputStream output = new FileOutputStream(path);
            workbook.write(output);
            output.close();
            path = excelUrl.substring(0, excelUrl.lastIndexOf("/")) + "/" + path.substring(originalPath.length() + 1);
            resultBody.setMessage("导出成功");
            resultBody.setCode("1");
            resultBody.setData(path);
        } catch (Exception e) {
            e.printStackTrace();
            resultBody.setMessage("导出失败");
            resultBody.setCode("-1");
        }
        return resultBody;
    }

}
